import {Form, Button, Radio, Col, DatePicker, TimePicker, Spin, Input } from 'antd';
import {WeaForm, WeaSwitch} from 'comsMobx';
import mapKeys from 'lodash/mapKeys';
import {WeaInput, WeaBrowser, WeaDialog, WeaNewScroll, WeaTimePicker, WeaTableEdit, WeaSearchGroup, WeaFormItem, WeaTools } from 'ecCom';
import _transform from 'lodash/transform'
import * as Apis from '../../../../apis/previewMeeting';
import moment from 'moment';
import MeetingMemberReceiptTable from './MeetingMemberReceiptTable'
import {WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
const FormItem = Form.Item;
const RadioGroup = Radio.Group;
import { inject, observer } from 'mobx-react';
import { toJS } from 'mobx';

const getOtherMemberIds = function(users) {
    let ids = users ? users.reduce((sum, value) => {
        return sum ? (sum + "," + value.id) : value.id;
    }, "") : "";
    return ids;
}

@inject('meetingDialog')
@observer
class MeetMemberReceiptDialog extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            visible: false,
            record: {},
            type: "",//类型，标识客户还是员工
            saveLoading: false,//保存中
            
        };
    }

    onChange = (data)=>{
        let { meetingId, meetingCalView, meetingDialog, } = this.props;
        let { getReceiptFormParams, receiptFormStatus, form , getFormParams} = meetingDialog;
        let {  form2, receiptFormCondition,token } =  receiptFormStatus
        let key = token
        const { type } = this.state
        let conditions = toJS(receiptFormCondition);
        let isFormInit = false;
       
        if (form[token]) {
            isFormInit = form[token].isFormInit
          }
        let value = {}
        if (form[token]) {
            value = form[token].getFormParams()
          }
        if(value['bookroom']==2){
            form[token].updateFields({
                roomstander:{
                    value:""
                }
            })
        }
        if(value['bookticket']==2){
            form[token].updateFields({
                tickettype:{
                    value:""
                },
                ticketstanderAir:{
                    value:''
                },
                ticketstanderTrain:{
                    value:''
                },
                backdate:{
                    value:''
                },
                backtime:{
                    value:""
                }
            })
        }
        if(value['isattend']==2){
            form[token].updateFields({
                tickettype:{
                    value:""
                },
                ticketstanderAir:{
                    value:''
                },
                ticketstanderTrain:{
                    value:''
                },
                backdate:{
                    value:''
                },
                backtime:{
                    value:""
                },
                begindate:{
                    value:""
                },
                begintime:{
                    value:""
                },
                bookroom:{
                    value:"2"
                },
                roomstander:{
                    value:""
                },
                bookticket:{
                    value:"2"
                }
            })
        }
        if(value['tickettype']==1){
            form[token].updateFields({
                ticketstanderAir:{
                    value:''
                }
              
            })
        }
        if(value['tickettype']==0){
            form[token].updateFields({
                ticketstanderTrain:{
                    value:''
                }
            })
        }
    }

    getSearchs = () => {
        let { meetingId, meetingCalView, meetingDialog, } = this.props;
        let { getReceiptFormParams, receiptFormStatus, form , getFormParams} = meetingDialog;
        let {  form2, receiptFormCondition,token } =  receiptFormStatus
        let key = token
        const { type } = this.state
        let conditions = toJS(receiptFormCondition);
        let isFormInit = false;
       
        if (form[token]) {
            isFormInit = form[token].isFormInit
          }
        let value = {}
        if (form[token]) {
            value = form[token].getFormParams()
          }
        let group = [];
        isFormInit && value && conditions && conditions.length!=0 && conditions.forEach((c, index) => {
          let items = []
          c && c.items && c.items.forEach(fields => {
                
                items.push({
                  com: (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@ynqaiw@${fields.domkey[0]}`}
                    label={`${fields.label}`}
                    labelCol={{ span: `${fields.labelcol}`, offset: '2' }}
                    wrapperCol={{ span: '11' }}
                    className={fields.domkey&&fields.domkey[0]}
                    underline
                    tipPosition='bottom'
                  >
                    <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@9p7717@${fields.domkey[0]}`} fieldConfig={fields} form={form[token]} formParams={value} onChange={this.onChange}/>
                  </WeaFormItem>),
                  colSpan: 1,
                  hide: form[token].isHide(fields, (keys, allParams) => {
                    let bool = false;
                    //相关联动
                    fields.domkey[0] === 'begindate' && value['isattend'] != 1 && (bool = true);
                    fields.domkey[0] === 'begintime' && value['isattend'] !=1 && (bool = true);
                    fields.domkey[0] === 'bookroom' && value['isattend'] !=1  && (bool = true);
                    fields.domkey[0] === 'roomstander' && value['isattend'] !=1 && (bool = true);
                    fields.domkey[0] === 'bookticket' && value['isattend'] !=1 && (bool = true);
                    fields.domkey[0] === 'tickettype' && value['isattend'] !=1 && (bool = true);
                    fields.domkey[0] === 'ticketstanderAir' && value['isattend'] !=1 && (bool = true);
                    fields.domkey[0] === 'ticketstanderTrain' && value['isattend'] !=1 && (bool = true);
                    fields.domkey[0] === 'backdate' && value['isattend'] !=1 && (bool = true);
                    fields.domkey[0] === 'backtime' && value['isattend'] !=1 && (bool = true);
                    // 订房联动
                    fields.domkey[0] === 'roomstander' && value['bookroom'] ==2 && (bool = true);
                    //回程联动
                    fields.domkey[0] === 'tickettype' && value['bookticket'] ==2 && (bool = true);
                    fields.domkey[0] === 'ticketstanderAir' && value['bookticket'] ==2 && (bool = true);
                    fields.domkey[0] === 'ticketstanderTrain' && value['bookticket'] ==2 && (bool = true);
                    fields.domkey[0] === 'backdate' && value['bookticket'] ==2 && (bool = true);
                    fields.domkey[0] === 'backtime' && value['bookticket'] ==2 && (bool = true);
                    fields.domkey[0] === 'ticketstanderAir' && value['tickettype'] ==1 && (bool = true);
                    fields.domkey[0] === 'ticketstanderTrain' && value['tickettype'] ==0 && (bool = true);
                    fields.domkey[0] === 'ticketstanderTrain' && value['tickettype'] =='' && (bool = true);
                    fields.domkey[0] === 'ticketstanderAir' && value['tickettype'] =='' && (bool = true);
                    return bool;
                  })
                })
            
            
          })
          
          group.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@iy4sqf@${index}`} col={1} needTigger={true} title={c.title} showGroup={c.defaultshow} items={items} />)
        });
        return group;
      }
    render(){
        const  { receipFormtVisible ,receiptFormStatus} = this.props.meetingDialog
        const { receiptFormCondition , isCrmReceiptDetail, crmCol, visible} = receiptFormStatus
        let { record, type,saveLoading} = this.state;
        return(
            <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@bci4fo`}
                visible={visible}
                title={getLabel(383354,"会议参与回执")}
                icon="icon-coms-meeting" iconBgcolor="#f14a2d"
                style={{width: 650, height: 450}}
                onCancel={this.back}
                hasScroll = 'true'
                buttons={[
                    <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@h6no8s@submit`} type="primary" onClick={this.submit}>{getLabel(383336,"提交")}</Button>,
                    <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@n57efi@reset`} type="primary" onClick={this.reset}>{getLabel(2022,"重置")}</Button>,
                ]}
            >
                <div className="receiptForm">
                <Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@6v0dg5`} spinning={saveLoading} style={{height: '100%'}}>

                    <WeaNewScroll ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScroll@bhjrg8`} height={"100%"}>
                    
                        {
                            this.getSearchs()
                            
                        }
                        {isCrmReceiptDetail?

                        <MeetingMemberReceiptTable ecId={`${this && this.props && this.props.ecId || ''}_MeetingMemberReceiptTable@rtc7m4`} columns={crmCol}/>:''

                        }
                    </WeaNewScroll>
                </Spin>
                </div>
            </WeaDialog>
        )
    }
    back = () =>{
        const  { receipFormtVisible ,receiptFormStatus,setReceiptFormStatus, clearForm,form} = this.props.meetingDialog
        const { form2, token } = receiptFormStatus
        const {type} = this.state
        clearForm(form[token])
        this.setState({
            visible: false,
            record: {}
        });
        setReceiptFormStatus({
            receiptFormCondition: [],
            oldReceiptFormCondition: [],
            isCrmReceiptDetail:false,
            crmCol:[],
            crmCondition:[],
            datas:[],
            formValue:{},
            visible:false
        })
    }
    submit = () =>{
        const  { form, getMemberListPageTree, dialogStatus, receipFormtVisible ,receiptFormStatus,setReceiptFormStatus, clearForm, getReceiptFormParams, other_member} = this.props.meetingDialog
        const { form2,datas, token } = receiptFormStatus
        const { meetingId } = dialogStatus;

        const {record, type} = this.state;
        let recorderid = record.recorderid
        let Datas = toJS(datas)
        let each = {};
        let crmRows = '';
        let formValue = getReceiptFormParams(form[token])
        if(formValue['isattend']==2){
           formValue = {...formValue,bookroom:"",bookticket:""}
        }
        if(formValue['tickettype']==0){
            formValue = {...formValue,"ticketstander":formValue['ticketstanderAir']}
        }else if(formValue['tickettype']==1){
            formValue = {...formValue,"ticketstander":formValue['ticketstanderTrain']}
        }
        // if(formValue['bookroom']==2){
        //     formValue = mapKeys(formValue, (value, key) => {
        //         if(key=='roomstander'){
        //             return key+'_off'
        //         }else {
        //             return key
        //         }
        //     })
        // }
        // if(formValue['bookticket']==2){
        //     formValue = {...formValue,tickettype:"",ticketstander:'',backdate:'',backtime:''}
        // }
        if(type=='crm'){
            crmRows = Datas && Datas.length;
            Datas && Datas instanceof Array && Datas.forEach((element, index) => {
                  each = mapKeys(element, (value, key) => {
                      return key + '_' + (index)
                  })
                  formValue = {...formValue,...each};
              })
            
          }
     
        let account = WeaTools.ls.getJSONObj('theme-account');
        formValue = {...formValue,membercrmrows:crmRows,meetingid:meetingId,recorderid:recorderid?recorderid:account.userid}
        this.setState({
            saveLoading:true
        })
        let promise = type == "crm"?Apis.meetingReCrm(formValue):Apis.meetingReHrm(formValue); 
        promise.then(result => {
            if(type === "crm"){
                getMemberListPageTree({meetingid:meetingId,type:2})
                
            }else{
                getMemberListPageTree({meetingid:meetingId,type:1})
            }
            this.setState({
                saveLoading:false
            });
            this.back();
        });

    }
    reset = () =>{
        const { form, receipFormtVisible ,receiptFormStatus,setReceiptFormStatus, clearForm} = this.props.meetingDialog
        const { token } = receiptFormStatus
        const { type } = this.state
            form[token].updateFields({
                isattend:{
                    value:"1"
                }, 
                begindate:{
                    value:""
                }, 
                begintime:{
                    value:""
                },
                bookroom:{
                    value:"2"
                },
                roomstander:{
                    value:""
                },
                bookticket:{
                    value:"2"
                },
                tickettype:{
                    value:""
                },
                ticketstanderAir:{
                    value:''
                },
                ticketstanderTrain:{
                    value:''
                },
                backdate:{
                    value:''
                },
                backtime:{
                    value:""
                },
                recRemark:{
                    value:""
                },
                othermember:{
                    value:''
                }
            })
        if(type=='crm'){
            setReceiptFormStatus({datas:[]})
        }
    }

    open = (record, type) => {
        const  { receipFormtVisible ,receiptFormStatus,setReceiptFormStatus, clearForm} = this.props.meetingDialog
        setReceiptFormStatus({visible:true})
        this.setState({
            visible: true,
            record: record,
            type: type
        });
      }
}


export default MeetMemberReceiptDialog;